// shopMallPackage/pages/score/scoreWithdraw/scoreWithdraw.js
import { scoreWithDrayRequest, getMyWallet } from "../../../../service/onetoone_service"
import debounce from "../../../../tools/debounce"

const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    drawMoney: 0, // 提现金额
    bankName: "",
    bankAccount: "",// 银行卡号
    bankAccountShow: "", // 显示银行卡号
    balanceMoney: 0,
    roleConfig: {},// 角色配置项
    allWithDrawMoney: 0,//全部可提现金额

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (app.globalData.userPayInfo.bankName) {
      this.setData({
        bankName: app.globalData.userPayInfo.bankName,
        bankAccount: app.globalData.userPayInfo.bankAccount || ""
      })
      if (app.globalData.userPayInfo.bankAccount) {
        let showText = this.data.bankAccount.slice(-4);
        let showTextStr = this.data.bankAccount.substring(0, 4)
        this.setData({
          bankAccountShow: "(" + showTextStr + "******" + showText + ")"
        })
      }
    }
    this.setData({
      roleConfig: app.globalData.roleConfig,
    })
    this.myWalletInfo();

  },



  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  inputAction(e) {
    this.setData({
      drawMoney: e.detail.value
    })
  },
  withDrawAction() {
    if (!this.data.bankAccount) {
      return;
    }
    if (!this.data.drawMoney) {
      wx.showToast({
        title: '请输入提现金额',
        icon: "none"
      })
      return;
    }
    if (this.data.drawMoney > this.data.allWithDrawMoney) {
      wx.showToast({
        title: '提现金额不能大于可兑换金额',
        icon: "none"
      })
      return;
    }
    debounce(this.scoreWithDrayRequest)
  },
  //提现操作:
  scoreWithDrayRequest() {
    wx.showLoading();
    scoreWithDrayRequest(this.data.drawMoney).then(res => {
      console.log("JJLog:提现成功===:" + JSON.stringify(res));
      wx.hideLoading();
      if (res.code == 200) {
        wx.showToast({
          title: '提现成功，等待打款',
          icon: "none"
        })
        this.setData({
          drawMoney: 0,
        })
        this.myWalletInfo();
      }
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err || "提现失败",
        icon: "none"
      })
      console.error("JJLog:scoreWithDrayRequest", err);
    })
  },
  pushToBankInfo() {
    wx.navigateTo({
      url: '/formPackage/pages/personalInfo/personalInfo',
    })
  },


  //获取我的积分数据:
  myWalletInfo() {
    getMyWallet().then(res => {
      console.log("JJLog:钱包数据===:" + JSON.stringify(res));
      if (res.code == 200) {
        this.setData({
          balanceMoney: res.data.restMoney || 0,
          allWithDrawMoney: res.data.restMoney
        })
        let cashFeeRatio = 0;
        if (this.data.roleConfig.cashFeeRatio) {
          cashFeeRatio = this.data.roleConfig.cashFeeRatio;
        }
        // let setMoeny = Math.floor((this.data.balanceMoney * (100 - cashFeeRatio)) / 100)
        // this.setData({
        //   allWithDrawMoney: setMoeny,
        // })
        app.globalData.balanceMoney = this.data.balanceMoney;
      }
    }).catch(err => {
      console.error("JJLog:getMyWallet", err);
    })
  },
  inputAllAction() {
    this.setData({
      drawMoney: this.data.allWithDrawMoney
    })
  },

  // 跳转到
  pushToIncomeList() {
    wx.navigateTo({
      url: '/storePackage/pages/calculusDetail/calculusDetail',
    })
  },



})